package com.imut.lagain.controller;

import com.imut.lagain.common.Result;
import com.imut.lagain.service.PerformanceMonitorService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 性能监控控制器
 */
@Tag(name = "性能监控", description = "系统性能监控相关接口")
@RestController
@RequestMapping("/performance")
@RequiredArgsConstructor
public class PerformanceController {
    private static final Logger log = LoggerFactory.getLogger(PerformanceController.class);


    private final PerformanceMonitorService performanceMonitorService;

    @Operation(summary = "获取系统性能指标")
    @GetMapping("/system")
    public Result<Map<String, Object>> getSystemMetrics() {
        Map<String, Object> metrics = performanceMonitorService.getSystemMetrics();
        return Result.success(metrics);
    }

    @Operation(summary = "获取数据库性能指标")
    @GetMapping("/database")
    public Result<Map<String, Object>> getDatabaseMetrics() {
        Map<String, Object> metrics = performanceMonitorService.getDatabaseMetrics();
        return Result.success(metrics);
    }

    @Operation(summary = "获取缓存性能指标")
    @GetMapping("/cache")
    public Result<Map<String, Object>> getCacheMetrics() {
        Map<String, Object> metrics = performanceMonitorService.getCacheMetrics();
        return Result.success(metrics);
    }

    @Operation(summary = "获取API性能指标")
    @GetMapping("/api")
    public Result<Map<String, Object>> getApiMetrics() {
        Map<String, Object> metrics = performanceMonitorService.getApiMetrics();
        return Result.success(metrics);
    }

    @Operation(summary = "获取完整性能报告")
    @GetMapping("/report")
    public Result<Map<String, Object>> getPerformanceReport() {
        Map<String, Object> report = performanceMonitorService.getPerformanceReport();
        return Result.success(report);
    }

    @Operation(summary = "清理过期性能数据")
    @PostMapping("/cleanup")
    public Result<Void> cleanupExpiredData() {
        performanceMonitorService.cleanupExpiredData();
        return Result.success();
    }
}
